<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>计划上报月计划表管理</title>
<meta name="decorator" content="default"/>
<link href="${ctxStatic}/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet" />
<link href="${ctxStatic}/bootstrap-table/bootstrap-table.min.css" rel="stylesheet" />
<script src="${ctxStatic}/bootstrap-editable/js/bootstrap-editable.js"></script>
<script src="${ctxStatic}/bootstrap-table/bootstrap-table.js"></script>
<script src="${ctxStatic}/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<script src="${ctxStatic}/bootstrap-table/extensions/editable/bootstrap-table-editable.js"></script>
<script src="${ctxStatic}/common/format/formatOrderData.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('#tab').bootstrapTable({
		url: "${ctx}/plan/bsPlanMonthDetail/formView?planMonthId=${bsPlanMonth.id}&storecode=${bsPlanMonth.fromOrgCode}&planDate="+$("#planDate").val(),
		//toolbar: "#toolbar",
		clickToSelect: true,
		uniqueId: "id",
		columns: [{
			field: 'id',
			title: 'id',
			visible: false
		}, {
			field: 'vaccineid',
			title: '疫苗小类',
			visible: false
		}, {
			field: 'vaccname',
			title: '疫苗名称'
		}, {
			field: 'type',
			formatter: formatSort,
			title: '类型'
		}, {
			field: 'spec',
			title: '规格'
		}, {
			field: 'dosage',
			title: '免疫程序剂次数'
		}, {
			field: 'lossFactor',
			title: '损耗系数'
		}, {
			field: 'packNum',
			title: '最小分包装数',
			width: '150px',
			editable: {
				type: 'text',
				emptytext: '0',
				disabled: true,
				mode: 'inline',
				inputclass: 'input-mini',
				validate: function(v) {
					var sellprice_reg = /^\d+(\.\d+)?$/;
					if (!sellprice_reg.test(v)) {
						return '输入不合法';
					}
				}
			}
		}, {
			field: 'planNumYear',
			title: '年计划领取数'
		}, {
			field: 'planNumMonth',
			title: '月度计划数'
		}, {
			field: 'storenum',
			title: '库存数量'
		}, {
			field: 'storespec',
			title: '库存剂次数'
		}, {
			field: 'sumRealNumMonth',
			title: '统计全站点上报计划数'
		}, {
			field: 'realNumMonth',
			title: '实际领取数',
			width: '150px',
			editable: {
				type: 'text',
				emptytext: '0',
				disabled: true,
				defaultValue: '0',
				mode: 'inline',
				inputclass: 'input-mini',
				validate: function(v) {
					var sellprice_reg = /^\d+(\.\d+)?$/;
					if (!sellprice_reg.test(v)) {
						return '输入不合法';
					}
				}
			},
			formatter: formatVaccname
		}, {
			field: 'planNumYearRemain',
			title: '年度累计领取数'
		},{
			field: 'remarks',
			title: '备注',
			width: '150px',
			editable: {
				type: 'text',
				emptytext: '--',
				disabled: true,
				mode: 'inline',
				inputclass: 'input-mini',
				validate: function(v) {
					
				}
			}
		}, {
			field: 'status',
			title: '是否更改',
			visible: false
		}, {
			field: 'statusOut',
			title: '是否更改替换',
			visible: false
		}],
		onEditableSave: function(field, row, oldValue, $el) {
			debugger;
			if(field == 'realNumMonth'){
				row.statusOut = 1;
				$('#tab').bootstrapTable('updateByUniqueId', {
					id: row.id,
					'row': row
				});
				$('.editable').editable('toggleDisabled');
			}
			setTimeout(function() {
				$('#tab').find('[data-uniqueid="' + row.id + '"]').next().find('[data-name="' + field + '"]').editable('show');
			},
			200);
		}
	});
	
	
	$("#btnAuditSun").attr("disabled",true);
	$("#btnSun").click(function(){//审核
		$('.editable').editable('toggleDisabled');
		$("#btnSun").attr("disabled",true);
		$("#btnAudit").attr("disabled",true);
		$("#btnAuditFail").attr("disabled",true);
		$("#btnAuditSun").attr("disabled",false);
	});
	$("#btnAuditSun").click(function(){//审批失败
		$("#inputForm").attr("action", "${ctx}/plan/bsPlanMonth/auditSun?success=true");
		inputFormBtn();
		$("#inputForm").submit();
	});
	$("#btnAudit").click(function(){//审批成功
		layer.confirm('审批通过?', {icon: 3, title:'提示'}, function(index){
			layer.load(2);
			$("#inputForm").attr("action", "${ctx}/plan/bsPlanMonth/audit?success=true");
			$("#inputForm").submit();
			layer.close(index);
		});
	});
	$("#btnAuditFail").click(function(){//审批失败
		layer.confirm('审批不通过？', {icon: 3, title:'提示'}, function(index){
			layer.load(2);
			$("#inputForm").attr("action", "${ctx}/plan/bsPlanMonth/audit?success=false");
			$("#inputForm").submit();
			layer.close(index);
		});
	});
	$("#btnReport").click(function(){
		var loadIndex = layer.load(2);
		var param = {
			"fromOrgCode" : $("#fromOrgCode").val(),
			"planDate" : $("#planDate").val(),
			"toOrgCode" : $("#toOrgCodeId").val()
		};
		$.post("${ctx}/plan/bsPlanMonth/checkPlanMonth", param, function(result){
			layer.close(loadIndex);
			if (result.success == false){
				layer.alert(result.msg, {icon: 2});
			} else if(result.success == true){
				layer.confirm('确认上报？', {icon: 3, title:'提示'}, function(index){
					layer.load(2);
					$("#inputForm").attr("action", "${ctx}/plan/bsPlanMonth/report?success=true");
					$("#inputForm").submit();
					layer.close(index);
				});
			}
		});
	});
});

function formatVaccname(data, row, index){
	var status = row.status;
	if(status == '1'){
		data = '('+data+')';
	}
	return data;
};

function postOrderDetil(form) { //表单保存时  将此方法中的数据尽行保存
	var rows = $('#tab').bootstrapTable('getData');
	$("#tableData").val(JSON.stringify(rows));
	layer.confirm('确定保存？', {
		btn : [ '保存', '取消' ]
	}, function() {
		layer.load(2);
		loading('正在提交，请稍等...');
		form.submit();
	});
}

function inputFormBtn(){
	$("#inputForm").validate({
		submitHandler: function(form){
			postOrderDetil(form);
		},
		errorContainer: "#messageBox",
		errorPlacement: function(error, element) {
			$("#messageBox").text("输入有误，请先更正。");
			if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
				error.appendTo(element.parent().parent());
			} else {
				error.insertAfter(element);
			}
		}
	});
}

</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/plan/bsPlanMonth/">月度计划列表</a></li>
		<li class="active"><a href="#">月度计划查看</a></li>
	</ul>
	<form:form id="inputForm" modelAttribute="bsPlanMonth" action="${ctx}/plan/bsPlanMonth/save" method="post" class="breadcrumb form-search">
		<form:hidden path="id" />
		<form:hidden path="planYearId" />
		<form:hidden path="fromOrgCode" />
		<form:hidden path="type" />
		<form:hidden path="planStatus" />
		<form:hidden path="planTitle" />
		<input id="tableData" name="tableData" type="hidden">
		<sys:message content="${message}" />
		<table class="table table-bordered">
			<tr>
				<th>填报单位：</th>
				<td><input readonly type="text" name="fromOrgName" id="fromOrgName" class="input-medium" value="${bsPlanMonth.fromOrgName}" /></td>
				<th>填报人：</th>
				<td><input type="text" name="planBy" id="planBy" readonly class="input-medium" value="${bsPlanMonth.planBy}" /></td>
				<th>审核单位：</th>
				<td><sys:treeselect2 id="toOrgCode" name="toOrgCode" value="${bsPlanMonth.toOrgCode}" labelName="toOrgName" disabled = "disabled"
						labelValue="${bsPlanMonth.toOrgName}" title="部门" url="/sys/office/treeData2?areaType=3" cssClass="input-medium"
						allowClear="true" notAllowSelectParent="true" /></td>
				<th>计划日期：</th>
				<td><input name="planDate" id="planDate" type="text" readonly disabled
					maxlength="20" class="input-medium Wdate "
					value="<fmt:formatDate value="${bsPlanMonth.planDate}" pattern="yyyy-MM"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM',isShowClear:false});" /></td>
			</tr>
			<tr>
				<th>是否强制确认：</th>
				<td>
					<form:select path="affirmStatus" class="input-medium">
						<form:option value="0" label="否"/>
						<form:option value="1" label="是"/>
					</form:select>
				</td>
				<td colspan="6"></td>
			</tr>
		</table>
		<div>
			<shiro:hasPermission name="plan:bsPlanMonth:edit">
				<c:if test="${bsPlanMonth.toOrgCode == office.code && bsPlanMonth.planStatus == 1}">
					<button id="btnSun" class="btn btn-primary" type="button">审核</button>
					<button id="btnAuditSun" class="btn btn-primary" type="button">保存并待确认</button>
					<button id="btnAudit" class="btn btn-primary" type="button">通过</button>
					<button id="btnAuditFail" class="btn btn-primary" type="button">退回</button>
				</c:if>
			</shiro:hasPermission>
			<shiro:hasPermission name="plan:bsPlanYear:report">
				<c:if test="${bsPlanMonth.fromOrgCode == office.code && (bsPlanMonth.planStatus == 0 || bsPlanMonth.planStatus == 9)}">
					<button id="btnReport" class="btn btn-primary" type="button">上报</button>
				</c:if>
			</shiro:hasPermission>
			<button id="btnCancel" class="btn" type="button" onclick="history.go(-1)">返回</button>
		</div>
	</form:form>
	<div class="control-group table">
		<fieldset>
		    <legend>${bsPlanMonth.planTitle}</legend>
		</fieldset>
		<table id="tab" class="table table-bordered table-condensed">
		</table>
	</div>
</body>
</html>